//################################
// 版权所有：中国农业银行软件开发中心
// 系统名称：移动办公应用-手机版
// 文件名称：index.js
// 创建时间：2016年07月03日
// 创建人员：Panda
// 功能说明：Button组件
//################################

'use strict';

import React, {Component} from 'react';
import {
    TouchableOpacity,
    StyleSheet,
    View,
    Text,
	InteractionManager,
} from 'react-native';

class Button extends Component {
    static propTypes = {
		visible: React.PropTypes.bool.isRequired,
		text: React.PropTypes.string.isRequired,
		onPress: React.PropTypes.func.isRequired,
	}

    static defaultProps = {
		visible: true,
		text: '',
        onPress: () => null,
	}

    constructor(props) {
		super(props);
		this.state = {
			isVisible: this.props.visible,
		};
	}

    componentWillReceiveProps(nextProps) {
		if (nextProps && nextProps.visible) {
			this.setState({
				isVisible: nextProps.visible,
			});
		}
	}

    _onPress() {
		InteractionManager.runAfterInteractions(() => {
			if (this.props && this.props.onPress) {
				this.props.onPress();
			}
		});
	}

    render() {
        if (this.state.isVisible === true) {
			return (
				<TouchableOpacity onPress={this._onPress.bind(this)} style={ [styles.ButtonView, this.props.style] }>
					<Text style={ [styles.ButtonViewText, this.props.textStyle] }>
						{this.props.text}
					</Text>
				</TouchableOpacity>
			);
		}
		else {
			return (
				<View />
			);
		}
    }
}

const styles = StyleSheet.create({
	ButtonView: {
        alignSelf: 'center',
		justifyContent: 'center',
        borderColor: '#37B9D0',
        backgroundColor: '#37B9D0',
        borderWidth: 1,
        marginTop: 4,
        marginBottom: 4,
        borderRadius: 3,
		alignItems: 'center',
    },

    ButtonViewText: {
		flex: 1,
		fontSize: 14,
        textAlign: 'center',
		textAlignVertical: 'center',
        color: '#FFFFFF',
        fontWeight: 'bold',
		alignSelf: 'center',
		justifyContent: 'center',
    },
});

export default Button;